<template>
  <div class="usersView">
    <div class="usersView-search">
      <h4>查询条件</h4>
      <el-input
        v-model="username"
        style="width: 200px;margin-right: 20px;"
        placeholder="请输入用户名"
        :prefix-icon="Search"
      />
      <el-button type="primary" @click="handleSearch" :icon="Search">开始查询</el-button>
      <el-button type="danger" @click="handleReset" :icon="Refresh">重置查询</el-button>
    </div>
    <div class="usersView-table">
      <div style="display: flex;justify-content: space-between;">
        <h4>用户列表</h4>
        <el-button @click="handleAdd" type="success">添加用户</el-button>
      </div>
      <el-table
        border
        :data="tableData"
        style="width: 100%"
      >
        <el-table-column prop="username" label="用户名" align="center" />
        <el-table-column prop="password" label="密码" align="center" />
        <el-table-column prop="account" show-overflow-tooltip label="账户信息" align="center" />
        <el-table-column prop="createTimeAt" show-overflow-tooltip label="创建时间" align="center" />
        <el-table-column fixed="right" label="操作" width="120" align="center">
          <template #default="scope">
            <el-button
              type="danger"
              size="small"
              @click.prevent="deleteRow(scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog lock-scroll v-model="dialogFormVisible" title="添加用户" :append-to-body="true" width="500">
      <el-form :model="forms">
        <el-form-item label="用户名" label-width="140px">
          <el-input v-model="forms.username" maxlength="6" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" label-width="140px">
          <el-input v-model="forms.password" maxlength="8" autocomplete="off" />
        </el-form-item>
        <el-form-item label="账户信息" label-width="140px">
          <el-input v-model="forms.account" maxlength="12" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleFormClose">取消</el-button>
          <el-button type="primary" @click="handleFormSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { Refresh, Search } from '@element-plus/icons-vue';
import { ElMessage, ElMessageBox, Action } from 'element-plus';
import axios from '../../utils/axios';
import { defineComponent, reactive, ref, onMounted } from "vue";

export default defineComponent({
  setup () {
    const forms = reactive({
      password: '',
      username: '',
      account: '',
      createTimeAt: new Date()
    });
    const username = ref('');
    const dialogFormVisible = ref(false);
    const tableData = ref([])
    const handleSearch = () => {
      const names = username.value
      if (!names) {
        ElMessage({
          showClose: true,
          message: '请输入用户名!',
          type: 'warning'
        })
        return
      }
      axios.get('http://localhost:3000/users/pronames', { params: {names} }).then(res => {
        if (res.data.code === 200) {
          console.log(res)
          tableData.value = res.data.data;
          ElMessage({
            showClose: true,
            message: res.data.msg,
            type: 'success'
          })
          return
        }
        console.log(res)
        ElMessage({
          showClose: true,
          message: res.data.msg,
          type: 'warning'
        })
        tableData.value = []
      })
    }
    const handleReset = () => {
      getloadData();
      username.value = '';
    }
    const handleAdd = () => {
      dialogFormVisible.value = true
    }
    const deleteRow = (row: string) => {
      ElMessageBox.confirm(
        '您是否确定删除当前用户?',
        '提示',
        {
          distinguishCancelAndClose: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }
      )
      .then(() => {
        axios.delete(`http://localhost:3000/users/${row}`).then(res => {
          getloadData()
          if (res.data.code === 200) {
            ElMessage({
              showClose: true,
              message: res.data.msg,
              type: 'success'
            })
            return
          }
          ElMessage({
            showClose: true,
            message: res.data.msg,
            type: 'error'
          })
        })
      })
      .catch((action: Action) => {
        console.log(action)
        ElMessage({
          showClose: true,
          message: '关闭弹框',
          type: 'info'
        })
      })
    }
    const handleFormClose = () => {
      getloadData();
      forms.username = '';
      forms.password = '';
      forms.account = '';
      dialogFormVisible.value = false;
    }
    const handleFormSubmit = () => {
      if (!forms.username) {
        ElMessage({
          showClose: true,
          message: '请输入用户名!',
          type: 'warning'
        })
        return
      }
      if (!forms.password) {
        ElMessage({
          showClose: true,
          message: '请输入密码!',
          type: 'warning'
        })
        return
      }
      if (!forms.account) {
        ElMessage({
          showClose: true,
          message: '请输入账户信息!',
          type: 'warning'
        })
        return
      }
      forms.createTimeAt = new Date()
      axios.post('http://localhost:3000/users', { ...forms }).then(res => {
        dialogFormVisible.value = false;
        getloadData();
        if (res.data.code === 200) {
          ElMessage({
            showClose: true,
            message: res.data.msg,
            type: 'success'
          })
          return
        }
        ElMessage({
          showClose: true,
          message: res.data.msg,
          type: 'error'
        })
      })
    }
    // 获取用户数据
    onMounted(() => {
      getloadData()
    })
    const getloadData = () => {
      const params = {
        page: 1,
        limit: 10,
      };
      axios.get('http://localhost:3000/users', { params }).then(res => {
        if (res.data.code === 200) {
          tableData.value = res.data.data
        }
      })
    }

    return {
      forms,
      username,
      dialogFormVisible,
      handleSearch,
      handleReset,
      handleAdd,
      Refresh,
      Search,
      deleteRow,
      getloadData,
      tableData,
      handleFormClose,
      handleFormSubmit
    }
  }
})
</script>
<style lang="scss" scoped>
h4{
  padding: 0 0 20px 0;
  font-size: 18px;
  color: #333333;
}
.usersView-search{
  padding-bottom: 20px;
  border-bottom: 1px solid #0F5185;
}
.usersView-table{
  margin-top: 20px;
}
</style>
